package com.endbd.purchase.manage.product.service.impl;


import com.endbd.feignapi.model.purchase.CProduct;
import com.endbd.feignapi.model.purchase.CType;
import com.endbd.feignapi.util.PageModel;
import com.endbd.purchase.manage.firm.dao.IFirmDao;
import com.endbd.purchase.manage.product.dao.IProductDao;
import com.endbd.purchase.manage.product.model.ProductCondition;
import com.endbd.purchase.manage.product.service.IProductService;
import com.endbd.purchase.manage.type.dao.ITypeDao;
import com.endbd.purchase.manage.type.model.TypeCondition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProductService implements IProductService {

    @Autowired
    private IProductDao productDao;
    @Autowired
    private ITypeDao typeDao;
    @Autowired
    private IFirmDao firmDao;

    @Override
    public PageModel<CProduct> queryProductPageByCondition(PageModel<CProduct> pageModel, ProductCondition pcon) {


        int pageNum = pageModel.getPageNum()<=0?1:pageModel.getPageNum();
        int pageSize = pageModel.getPageSize()<=0?5:pageModel.getPageSize();

        PageHelper.startPage(pageNum,pageSize);
        List<CProduct> products = productDao.queryProductCondition(pcon);

        PageInfo<CProduct> info = new PageInfo<CProduct>(products);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(products);
        return pageModel;
    }

    @Override
    public void addProduct(CProduct product, String brandName, String typeName,String firmName) {
        getId(product,brandName,typeName,firmName);
        productDao.addProduct(product);
    }

    @Override
    public void alterProduct(CProduct product, String brandName, String typeName, String firmName){
        getId(product,brandName,typeName,firmName);
        productDao.alterProduct(product);
    }

    @Override
    public void lor(ProductCondition pcon) {
        if (pcon.getProductStatus()==1){
            productDao.logProduct(pcon.getProductId());
        }else {
            productDao.recoveryProduct(pcon.getProductId());
        }
    }

    public void getId(CProduct product, String brandName, String typename,String firmName){
        //通过brandName与typeName获取brandId并赋值到product中
        TypeCondition tcon = new TypeCondition();
        tcon.setBrandName(brandName);
        tcon.setTypeName(typename);
        List<CType> cTypes = typeDao.queryTypeCondition(tcon);
        product.setTypeId(cTypes.get(0).getTypeId());
        //通过firmName获取firmId并赋值到product中
        product.setFirmId(firmDao.firmId(firmName));

    }


}
